package com.csi.bumblebee.controller;

import com.csi.bumblebee.domain.dictionary.JobExam;
import com.csi.bumblebee.domain.user.ExamResult;
import com.csi.bumblebee.domain.util.MyResult;
import com.csi.bumblebee.service.ExamResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.csi.bumblebee.domain.util.StateCode.*;

/**
 * @author HUOJT
 */
@RestController
@RequestMapping("examresult")
public class ExamResultController {

    @Autowired
    private ExamResultService examResultService;

    /**
     * 判卷并放入数据库
     *
     * @param examResult 带岗位类型的做题结果
     * @return 是否成功
     */
    @PostMapping("paper")
    public MyResult<String> makeScore(@RequestBody ExamResult examResult) {
        System.out.println(" +++++++++++ " + examResult.getStudentId());
        System.out.println(" ------------ " + examResult.getJobTypeId());
        for (JobExam jobExam : examResult.getJobType().getJobExamList()) {
            System.out.println(jobExam.toString());
        }
        MyResult<String> myResult = new MyResult<>(INSERT_FAILURE, "增添失败");
        String id = examResultService.makeScore(examResult);
        if (id != null) {
            myResult.setData(id);
            myResult.setCode(INSERT_SUCCESS);
            myResult.setMessage("添加成功");
        }
        return myResult;
    }

    /**
     * 得到
     *
     * @param id 结果id
     * @return 结果
     */
    @GetMapping("paper/{id}")
    public MyResult<ExamResult> getPaperResult(@PathVariable("id") String id) {
        MyResult<ExamResult> myResult = new MyResult<>(FIND_FAILURE, "查询失败");
        ExamResult examResult = examResultService.getByExamResultId(id);
        if (examResult != null) {
            myResult.setData(examResult);
            myResult.setCode(FIND_SUCCESS);
            myResult.setMessage("查询成功");
            System.out.println("+++++++++++"+myResult.toString());
        }
        return myResult;
    }

}
